import { checkCreateSuppBillTask, getSuppBillProgress } from '@/services/chargemanage';

export default {
  state: {
    isHasTask: false,
    billTaskId: null,
    taskCode: null,
    progressPercent: 0,
    selectMessage: [],
    formFieldsValue: { defaultFlag: '1' },
    isdefaultFlag: '1',
  },
  effects: {
    // 判断有无账单任务
    *checkCreateBillTask(_, { call, put }) {
      const response = yield call(checkCreateSuppBillTask);
      if (response.success && response.data) {
        if (response.data.taskCode === 3) {
          yield put({
            type: 'updateModelData',
            payload: {
              isHasTask: true,
              billTaskId: response.data.billTaskId,
              taskCode: response.data.taskCode,
            },
          });
        } else {
          yield put({
            type: 'updateModelData',
            payload: {
              isHasTask: false,
            },
          });
        }
      }
    },
    // 获取进度数据
    *getProgress({ payload }, { call, put }) {
      const response = yield call(getSuppBillProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { progressPercent: response.data.rate },
        });
      }
      return response;
    },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    defaultFlagModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 选中的数据
    getSelectedData(state, { payload }) {
      // eslint-disable-next-line array-callback-return
      payload.map(item => {
        state.selectMessage.push(item);
      });
      return {
        ...state,
        selectMessage: state.selectMessage,
      };
    },
  },
};
